﻿using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {
	private void Start(){
		findPosition();
	}

	private void walkRepeat(){
		InvokeRepeating("enemyWalk", 3, 5f);
		//enemyWalk();
	}


	//Variables
	public Transform enemy;

	private Vector2 position;

	/* not necessary
	private float maxPosX;
	private float maxPosY;
	private float maxNegX;
	private float maxNegY;
	*/

	public float wanderSpeed = 3;

	private float totalWalkDistanceUp;
	private float totalWalkDistanceDown;
	private float totalWalkDistanceLeft;
	private float totalWalkDistanceRight;
	



	private void findPosition(){
		position.x = enemy.position.x;
		position.y = enemy.position.y;

		/* not neccessary
		maxPosX = position.x + 5;
		maxNegX = position.x - 5;
		maxPosY = position.y + 5;
		maxNegY = position.y -5;
		*/

		Debug.Log ("Enemy position: " + position);
		//Debug.Log ("Max positive 'x' position: " + maxPosX + ". Max positive 'y' position: " + maxPosY + ". Max negative 'x' position: " + maxNegX + ". Max negative 'y' position: " + maxNegY);
		Debug.Log ("Finished!");

		walkRepeat();
	}

	private void enemyWalk(){
		//Distance Generator
		float walkDistance = Random.Range (40f, 70f);
		totalWalkDistanceUp = walkDistance + position.y;
		totalWalkDistanceDown = position.y - walkDistance;
		totalWalkDistanceRight = walkDistance + position.x;
		totalWalkDistanceLeft = position.x - walkDistance;
		Debug.Log ("Distance generated up: " + totalWalkDistanceUp + ". Distance generated down: " + totalWalkDistanceDown + ". Distance generated left: " + totalWalkDistanceLeft + ". Distance generated right: " + totalWalkDistanceRight);

		//Direction generator
		int directionGenerator = Random.Range (1, 4);

		if(directionGenerator == 1){ //up
			enemy.transform.Translate(new Vector2(0, totalWalkDistanceUp) * wanderSpeed * Time.deltaTime);
			Debug.Log ("Direction generated: up");
		}
		else if(directionGenerator == 2){ //down
			enemy.transform.Translate(new Vector2(0, totalWalkDistanceDown) * wanderSpeed * Time.deltaTime);
			Debug.Log ("Direction generated: down");
		}
		else if(directionGenerator == 3){ //left
			enemy.transform.Translate (new Vector2(totalWalkDistanceLeft, 0) * wanderSpeed * Time.deltaTime);
			Debug.Log ("Direction generated: left");
		}
		else if(directionGenerator == 4){ //right
			enemy.transform.Translate(new Vector2(totalWalkDistanceRight,0) * wanderSpeed * Time.deltaTime);
			Debug.Log ("Direction generated: right");
		}
	}

}
